<?php
namespace App\Noxinfluencer\Controllers;
require_once '../../BaseController.php';
require_once '../../../vendor/autoload.php';

use App\BaseController;
use phpspider\core\requests;
use phpspider\core\selector;

class UserVideoSpiderController extends BaseController
{

    const PAGE_SIZE = 10000;

    public function __construct()
    {

    }

    public function getUserOfNotVideo(){
        $users = $this->medooDb()->select('kol_user', [
            "[>]kol_user_video" => ["id" => "user_id"],
        ], [
            'kol_user.id',
            'kol_user.channel_id',
            'kol_user_video.user_id',
        ], [
            "kol_user_video.user_id" => null,
            "ORDER" => ["kol_user.id" => "ASC"],
            "LIMIT" => 2000
        ]);
        return $users;
    }


    public function startCreateUserVideo()
    {
        $medooDb = $this->medooDb();

        while(true) {

            $users = $this->getUserOfNotVideo();

            if(empty($users)){
                echo "kol视频已同步完成\n";
                sleep(600);
            }

            foreach ($users as $key => $value) {
                $url = "https://cn.noxinfluencer.com/api/video/list?pageSize=".self::PAGE_SIZE."&channelId={$value['channel_id']}&offset=0&sortField=pub_date&isPredicted=0";

                $data = requests::get($url);

                $cotnet = json_decode($data, true);
                $html = $cotnet['retData']['dom'];

                $video_id = selector::select($html, "//div[@class='video-wrapper']/@data-video-id");

                $img = selector::select($html, "//img[@class='thumb']/@src");

                $title = selector::select($html, "//a[@class='underline']");

                $video_look_num_data = selector::select($html, "//div[@class='detail']//div[@class='subtitle']");

                $save_data = [];

                if(is_array($video_id)) {
                    for ($i = 0; $i < count($video_id); $i++) {
                        $video_look_num = explode(' 观看量 · ', $video_look_num_data[$i]);

                        $save_data[$i] = array(
                            'user_id' => $value['id'],
                            'video_source_id' => $video_id[$i],
                            'video_source_pic' => $img[$i],
                            'video_name' => $title[$i],
                            'video_look_num' => $this->formatNum($video_look_num[0]),
                            'video_upload_time' => str_replace([' 视频发布时间'], [''], $video_look_num[1]),
                            'create_time' => date('Y-m-d H:i:s', time()),
                            'update_time' => date('Y-m-d H:i:s', time()),
                        );
                    }

                    $medooDb->insert('kol_user_video', $save_data);
                }

                echo $value['id']."\n";
            }

            echo "\n一个周期结束\n";

            sleep(1);
        }

    }


    public function formatNum($data)
    {
        if(strpos($data,'万元')){
            return trim(str_replace(['万元'], [''], $data)) * 10000;
        }

        if(strpos($data,'万')){
            return trim(str_replace(['万'], [''], $data)) * 10000;
        }

        if(strpos($data,'亿')){
            return trim(str_replace(['亿'], [''], $data)) * 100000000;
        }

        return $data;
    }


}

(new UserVideoSpiderController())->startCreateUserVideo();





